/*
 * THIS IS A GENERATED FILE!  DO NOT CHANGE THIS FILE!  CHANGE THE
 * CORRESPONDING TEMPLATE FILE, PLEASE!
 */

#include <gtest/gtest.h>
#include <shogun/base/SGObject.h>
#include <shogun/base/class_list.h>
#include <shogun/io/SerializableJsonFile.h>
#include <unistd.h>
#include "utils/Utils.h"

using namespace shogun;

#ifdef HAVE_JSON
{% set ignores = ['RationalApproximationIndividualJob',
	'CCSOSVM',
	'TwoStateModel'] %}

{% for class in classes %}
{% if class in ignores or class.startswith('GUI') %}
TEST(SerializationJSON, DISABLED_{{class}})
{% else %}
TEST(SerializationJSON, {{class}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string filename = "shogun-unittest-serialization-json-" + class_name + ".XXXXXX";
	generate_temp_filename(const_cast<char*>(filename.c_str()));
	CSGObject* object = create(class_name.c_str(), PT_NOT_GENERIC);
	ASSERT_TRUE(object != NULL);

	// save object to an ascii file
	CSerializableJsonFile *file=new CSerializableJsonFile(filename.c_str(), 'w');
	bool save_success = object->save_serializable(file);
	file->close();
	SG_UNREF(file);
	ASSERT_TRUE(save_success);

	// load parameter from an ascii file
	file=new CSerializableJsonFile(filename.c_str(), 'r');
	CSGObject* deserializedObject = create(class_name.c_str(), PT_NOT_GENERIC);
	ASSERT_TRUE(deserializedObject != NULL);
	bool load_success = deserializedObject->load_serializable(file);
	file->close();
	SG_UNREF(file);
	ASSERT_TRUE(load_success);

	// check whether they are equal, up to accuracy since json is lossy
	float64_t accuracy=1e-6;
	ASSERT(object->equals(deserializedObject, accuracy, true));

	SG_UNREF(object)
	SG_UNREF(deserializedObject);

	int delete_success = unlink(filename.c_str());
	ASSERT_EQ(0, delete_success);
}
{% endfor %}

{% for class in template_classes %}
{% for type in types %}
{% if class in ignores %}
TEST(SerializationJSON,DISABLED_{{class}}_{{type}})
{% else %}
TEST(SerializationJSON,{{class}}_{{type}})
{% endif %}
{
	std::string class_name("{{class}}");
	std::string filename = "shogun-unittest-serialization-json-" + class_name + "_{{type}}" + ".XXXXXX";
	generate_temp_filename(const_cast<char*>(filename.c_str()));
	CSGObject* object = create(class_name.c_str(), {{type}});
	ASSERT_TRUE(object != NULL);

	// save object to an ascii file
	CSerializableJsonFile *file=new CSerializableJsonFile(filename.c_str(), 'w');
	bool save_success = object->save_serializable(file);
	file->close();
	SG_UNREF(file);
	ASSERT_TRUE(save_success);

	// load parameter from an ascii file
	file=new CSerializableJsonFile(filename.c_str(), 'r');
	CSGObject* deserializedObject = create(class_name.c_str(), {{type}});
	ASSERT_TRUE(deserializedObject != NULL);
	bool load_success = deserializedObject->load_serializable(file);
	file->close();
	SG_UNREF(file);
	ASSERT_TRUE(load_success);

	// check whether they are equal, up to accuracy since json is lossy
	float64_t accuracy=1e-6;
	ASSERT(object->equals(deserializedObject, accuracy, true));

	SG_UNREF(object)
	SG_UNREF(deserializedObject);

	int delete_success = unlink(filename.c_str());
	ASSERT_EQ(0, delete_success);
}
{% endfor %}
{% endfor %}

#endif /* HAVE_JSON */
